#include "mpu6050.h"
/**
  ******************************************************************************
  �ӼĴ����ж�����
  ******************************************************************************
	��Ĵ�����д����
  ******************************************************************************
	MPU6050��ʼ��
	******************************************************************************
  */

void MPU6050_ReadData(uint16_t reg_add, uint8_t* read,uint8_t num)
{
//	int i;
//	for(i=0;i<num;i++)
//	{
	 HAL_I2C_Mem_Read(&hi2c1, MPU_ADDR, reg_add, I2C_MEMADD_SIZE_8BIT, read, num, 10);	
//	 read++;
//	}
}
void MPU6050_WriteReg(uint8_t reg_add, uint8_t reg_dat)
{
	uint8_t data[2];
	data[0]=reg_add;
	data[1]=reg_dat;
	HAL_I2C_Master_Transmit(&hi2c1, MPU_ADDR, data, 2, 10);
}

void MPU6050_Init(void)
{
  //�ڳ�ʼ��֮ǰҪ��ʱһ��ʱ�䣬��û����ʱ����ϵ�����ϵ����ݿ��ܻ����
  HAL_Delay(1000);
	MPU6050_WriteReg(MPU_PWR_MGMT1_REG, 0x00);	  //�������״̬
	MPU6050_WriteReg(MPU_SAMPLE_RATE_REG, 0x07);	//�����ǲ����ʣ�1KHz
	MPU6050_WriteReg(MPU_CFG_REG, 0x06);	        //��ͨ�˲��������ã���ֹƵ����1K��������5K
	MPU6050_WriteReg(MPU_ACCEL_CFG_REG, 0x00);	  //���ü��ٶȴ�����������2Gģʽ�����Լ�
	MPU6050_WriteReg(MPU_GYRO_CFG_REG, 0x00);     //�������Լ켰������Χ������ֵ��0x00(���Լ죬250deg/s)
}

//��ȡmpu6050��ַ������˵��Ӳ��û���⣬������򴮿ڣ�usart1�����ͽ��
int MPU6050ReadID(void)
{
	uint8_t Re=0;
  MPU6050_ReadData(MPU_DEVICE_ID_REG,&Re,1);    //����ID�Ĵ���,who am i�Ĵ���
	if(Re != 0x68)
	{
		printf("MPU6050 dectected error!\r\n");
		printf("%u\n",Re);
		return 0;
	}
	else
	{
		//printf("MPU6050 ID = %u\r\n",Re);
		return 1;
	}
		//printf("aaaaaaaa%u \n",Re);
}

/**
  ******************************************************************************
  ���¶�(float Temperature)
  ******************************************************************************
	�����ٶ�(short Accel[3])
  ******************************************************************************
	�����ٶ�(short Gyro[3])
	******************************************************************************
  */

void MPU6050_ReturnTemp(float *Temperature)
{
	short temp3;
	uint8_t buf[2];
	uint8_t a;
	uint8_t b;
	MPU6050_ReadData(MPU_TEMP_OUTH_REG, buf, 2);//��ȡ�¶�ֵ
	temp3= (buf[0] << 8) | buf[1];
	*Temperature=((double) temp3/340.0)+36.53;

}

void MPU6050ReadAcc(short *accData)
{
    uint8_t buf[6];
		
    MPU6050_ReadData(MPU_ACCEL_XOUTH_REG, buf, 6);//��ȡ���ٶ�
    accData[0] = (buf[0] << 8) | buf[1];
    accData[1] = (buf[2] << 8) | buf[3];
    accData[2] = (buf[4] << 8) | buf[5];
}

void MPU6050ReadGyro(short *gyroData)
{
    uint8_t buf[6];
    MPU6050_ReadData(MPU_GYRO_XOUTH_REG, buf, 6);//��ȡ���ٶ�
    gyroData[0] = (buf[0] << 8) | buf[1];
    gyroData[1] = (buf[2] << 8) | buf[3];
    gyroData[2] = (buf[4] << 8) | buf[5];
}
